package com.rockcent.api.controller;

import com.rockcent.api.Routes;
import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.common.utils.RBeanUtils;
import com.rockcent.mall.lib.domain.Mall;
import com.rockcent.mall.lib.domain.MallMemberPayStandard;
import com.rockcent.mall.lib.dto.MallMemberPayStandardDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.MallMemberPayStandardRepository;
import com.rockcent.mall.lib.repository.MallRepository;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.stream.Collectors;

/**
 * Created by DK on 17/3/7.
 * 平台付费标准
 */
@RestController
@RequestMapping(value = Routes.API_VERSION)
public class MallMemberPayStandardController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(MallMemberPayStandardController.class);

    @Autowired
    private MallMemberPayStandardRepository mallMemberPayStandardRepository;
    @Autowired
    private MallRepository mallRepository;
    @Autowired
    private BaseUserHelper userHelper;

    /**
     * @api {get} /v1/mall/member/pay_standard/page R 平台付费标准分页
     * @apiGroup mall_member
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiParam {String} mallName 平台名称
     * @apiParam {String} mallId 平台ID
     *
     * @apiSuccess {long} id ID
     * @apiSuccess {long} mallId 平台ID
     * @apiSuccess {BigDecimal} baseServiceFee 基础服务费
     * @apiSuccess {BigDecimal} baseServiceFeeNum 基础服务费系数
     * @apiSuccess {BigDecimal} businessServiceFee 商户服务费/户
     * @apiSuccess {BigDecimal} businessServiceFeeNum 商户服务费系数
     * @apiSuccess {BigDecimal} dealResellRate 交易佣金率
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data" : [
     *          {
     *              "id" : 12
     *              "mallId" : 2,
     *              "baseServiceFee" : 2345,
     *              "baseServiceFeeNum" : "0.45",
     *              "businessServiceFee" : 23,
     *              "businessServiceFeeNum" : "0.4",
     *              "dealResellRate" : "0.6"
     *          },
     *          .......
     *   ]
     * }
     */
    @RequestMapping(value = Routes.MALL_MEMBER_PAY_STANDARD_PAGE, method = RequestMethod.GET)
    public JsonResult mallMemberPayStandardPage (HttpServletRequest request, MallMemberPayStandardDto dto) {
        RestDoing doing = jsonResult -> {
            PageResult<MallMemberPayStandardDto> pageResult = mallMemberPayStandardRepository.findMallMemberPayStandardPage(dto.getMallId(), dto.getMallName(), Whether.NO.toString(), this.getPage(dto.getPageNo(), dto.getPageSize()));
            pageResult.data.stream().map(mallMemberPayStandardDto -> {
                BigDecimal dealResellRate = new BigDecimal(mallMemberPayStandardDto.getDealResellRate()).multiply(BigDecimal.valueOf(100));
                mallMemberPayStandardDto.setDealResellRate(dealResellRate.toString());
                return mallMemberPayStandardDto;
            }).collect(Collectors.toList());
            pageResult.setUrl(Routes.MALL_MEMBER_PAY_STANDARD_PAGE);
            jsonResult.data = pageResult;
        };
        return doing.go(request, log);
    }

    /**
     * @api {get} /v1/mall/member/pay_standard/record/page R 平台付费标准历史分页
     * @apiGroup mall_member
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiParam {String} mallId <code>必传参数</code>平台ID
     *
     * @apiSuccess {long} id ID
     * @apiSuccess {long} mallId 平台ID
     * @apiSuccess {long} operatorId 操作人ID
     * @apiSuccess {BigDecimal} baseServiceFee 基础服务费
     * @apiSuccess {BigDecimal} baseServiceFeeNum 基础服务费系数
     * @apiSuccess {BigDecimal} businessServiceFee 商户服务费/户
     * @apiSuccess {BigDecimal} businessServiceFeeNum 商户服务费系数
     * @apiSuccess {String} dealResellRate 交易佣金率
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data" : [
     *          {
     *              "id" : 12
     *              "mallId" : 2,
     *              "baseServiceFee" : 2345,
     *              "baseServiceFeeNum" : "0.45",
     *              "businessServiceFee" : 23,
     *              "businessServiceFeeNum" : "0.4",
     *              "dealResellRate" : "0.6",
     *              "operatorId" : 123,
     *              "dealResellRate" : "0.6"
     *          },
     *          .......
     *   ]
     * }
     */
    @RequestMapping(value = Routes.MALL_MEMBER_PAY_STANDARD_RECORD_PAGE, method = RequestMethod.GET)
    public JsonResult mallPayStandardRecordByMallIdPage (HttpServletRequest request, MallMemberPayStandardDto dto) {
        RestDoing doing = jsonResult -> {
            if (dto.getMallId() == null) {
                jsonResult.remind("平台ID 不能为空",log);
                return;
            }
            PageResult<MallMemberPayStandardDto> pageResult = mallMemberPayStandardRepository.findMallMemberPayStandardPage(dto.getMallId(), dto.getMallName(), null, this.getPage(dto.getPageNo(), dto.getPageSize()));
            pageResult.data.stream().map(mallMemberPayStandardDto -> {
                BigDecimal dealResellRate = new BigDecimal(mallMemberPayStandardDto.getDealResellRate()).multiply(BigDecimal.valueOf(100));
                mallMemberPayStandardDto.setDealResellRate(dealResellRate.toString());
                return mallMemberPayStandardDto;
            }).collect(Collectors.toList());
            pageResult.setUrl(Routes.MALL_MEMBER_PAY_STANDARD_PAGE);
            jsonResult.data = pageResult;
        };
        return doing.go(request, log);
    }

    /**
     * @api {get} /v1/mall/member/pay_standard/info R 平台付费标准详情
     * @apiGroup mall_member
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiParam {String} id <code>必传参数</code>id
     *
     * @apiSuccess {long} id ID
     * @apiSuccess {long} mallId 平台ID
     * @apiSuccess {BigDecimal} baseServiceFee 基础服务费
     * @apiSuccess {BigDecimal} baseServiceFeeNum 基础服务费系数
     * @apiSuccess {BigDecimal} businessServiceFee 商户服务费/户
     * @apiSuccess {BigDecimal} businessServiceFeeNum 商户服务费系数
     * @apiSuccess {BigDecimal} dealResellRate 交易佣金率
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data" : [
     *          {
     *              "id" : 12
     *              "mallId" : 2,
     *              "baseServiceFee" : 2345,
     *              "baseServiceFeeNum" : "0.45",
     *              "businessServiceFee" : 23,
     *              "businessServiceFeeNum" : "0.4",
     *              "dealResellRate" : "0.6"
     *          },
     *          .......
     *   ]
     * }
     */
    @RequestMapping(value = Routes.MALL_MEMBER_PAY_STANDARD_INFO, method = RequestMethod.GET)
    public JsonResult mallPayStandardInfo(HttpServletRequest request, MallMemberPayStandardDto dto) {
        RestDoing doing = jsonResult -> {
            if (dto.getId() == null) {
                jsonResult.remind(MallMemberPayStandard.CHECK_ID_IS_NOT_NULL,log);;
                return;
            }
            MallMemberPayStandard mallPayStandard = mallMemberPayStandardRepository.findOne(dto.getId());
            if (mallPayStandard == null) {
                jsonResult.remind(MallMemberPayStandard.REMIND_RECORD_IS_NOT_EXIST,log);;
                return;
            }
            RBeanUtils.copyProperties(mallPayStandard, dto);
            BigDecimal dealResellRate = new BigDecimal(dto.getDealResellRate()).multiply(BigDecimal.valueOf(100));
            dto.setDealResellRate(dealResellRate.toString());
            Mall mall = mallRepository.findOne(mallPayStandard.getMallId());
            if (mall != null && StringUtils.isNotBlank(mall.getName())) {
                dto.setMallName(mall.getName());
            }
            jsonResult.data = dto;
        };
        return doing.go(request, log);
    }

    /**
     * @api {put} /v1/mall/member/pay_standard/update R 平台付费标准更改
     * @apiGroup mall_member
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiParam {long} id <code>必传参数</code> ID
     * @apiParam {BigDecimal} baseServiceFee <code>必传参数</code> 基础服务费
     * @apiParam {BigDecimal} baseServiceFeeNum <code>必传参数</code> 基础服务费系数
     * @apiParam {BigDecimal} businessServiceFee <code>必传参数</code> 商户服务费/户
     * @apiParam {BigDecimal} businessServiceFeeNum <code>必传参数</code> 商户服务费系数
     * @apiParam {BigDecimal} dealResellRate <code>必传参数</code> 交易佣金率
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *   "code": "1",
     *   "desc": "成功"
     */
    @RequestMapping(value = Routes.MALL_MEMBER_PAY_STANDARD_UPDATE, method = RequestMethod.PUT)
    public JsonResult mallPayStandardUpdate(HttpServletRequest request, @RequestBody MallMemberPayStandardDto dto) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            if (dto.getId() == null) {
                jsonResult.remind(MallMemberPayStandard.CHECK_ID_IS_NOT_NULL,log);;
                return;
            }

            if (dto.getBaseServiceFee() == null) {
                jsonResult.remind("基础服务费 不能为空",log);
                return;
            }
            if (dto.getBaseServiceFeeNum() == null) {
                jsonResult.remind("基础服务费系数 不能为空",log);
                return;
            }
            if (dto.getBusinessServiceFee() == null) {
                jsonResult.remind("商户服务费/户 不能为空",log);
                return;
            }
            if (dto.getBusinessServiceFeeNum() == null) {
                jsonResult.remind("商户服务费系数 不能为空",log);
                return;
            }
            if (dto.getDealResellRate() == null) {
                jsonResult.remind("交易佣金率 不能为空",log);
                return;
            }
            MallMemberPayStandard oldMallPayStandard = mallMemberPayStandardRepository.findByIdAndIsDelete(dto.getId(), Whether.NO);
            if (oldMallPayStandard == null) {
                jsonResult.remind(MallMemberPayStandard.REMIND_RECORD_IS_NOT_EXIST,log);;
                return;
            }

            BigDecimal dealResellRate = new BigDecimal(dto.getDealResellRate()).divide(BigDecimal.valueOf(100));
            MallMemberPayStandard newMallMemberPayStandard = new MallMemberPayStandard();
            newMallMemberPayStandard.setBaseServiceFee(dto.getBaseServiceFee());
            newMallMemberPayStandard.setBaseServiceFeeNum(dto.getBaseServiceFeeNum());
            newMallMemberPayStandard.setBusinessServiceFee(dto.getBusinessServiceFee());
            newMallMemberPayStandard.setBusinessServiceFeeNum(dto.getBusinessServiceFeeNum());
            newMallMemberPayStandard.setDealResellRate(dealResellRate.toString());
            newMallMemberPayStandard.setMallMemberId(oldMallPayStandard.getMallMemberId());
            newMallMemberPayStandard.setMallId(oldMallPayStandard.getMallId());
            newMallMemberPayStandard.setOperatorId(userInfoDto.getId());
            oldMallPayStandard.setIsDelete(Whether.YES);
            mallMemberPayStandardRepository.save(oldMallPayStandard);
            newMallMemberPayStandard = mallMemberPayStandardRepository.save(newMallMemberPayStandard);
            jsonResult.data = newMallMemberPayStandard;
        };
        return doing.go(request, log);
    }
}
